package com.zhx.real.vmcreate;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author zhanghaixuan
 * @date 2017/10/29
 */
public class AutoResourcesGenerator {

	private static String[] TABLES = new String[] {"visit_subdetail","visit_option_threshold","visit_option_threshold_real"};


	private static final String AUTHOR = "zhanghaixuan";

	public static void main(String[] args) {

		ResourceBundle resource = ResourceBundle.getBundle("mybatiscode");
		String outPutDir = resource.getString("outputDir");
		Boolean fileOverride = false;
		if("true".equals(resource.getString("fileOverride"))) {
			fileOverride = true;
		}
		String url = resource.getString("url");
		String driverClass = resource.getString("driverClass");
		String userName = resource.getString("userName");
		String passWord = resource.getString("passWord");
		String setParent = resource.getString("setParent");
		String mapperPath = resource.getString("mapperPath");

		//代码生成器
		AutoGenerator mpg = new AutoGenerator();

		//全局配置
		GlobalConfig gc = new GlobalConfig();
		//生成的文件输出到哪
		String projectPath = System.getProperty("user.dir");
		gc.setOutputDir(projectPath + outPutDir);
		gc.setIdType(IdType.ID_WORKER_STR);
		gc.setOpen(false);
		gc.setFileOverride(fileOverride);
		gc.setAuthor(AUTHOR);
		gc.setBaseColumnList(true);
		gc.setBaseResultMap(true);

		gc.setEntityName("%sEntity");
		gc.setMapperName("%sMapper");
		gc.setServiceName("%sService");
		gc.setServiceImplName("%sServiceImpl");
		gc.setControllerName("%sController");
		mpg.setGlobalConfig(gc);
		gc.setIdType(IdType.ID_WORKER_STR);
		//是否开启Swagger2
		//gc.setSwagger2(true);
		//gc.setXmlName("%sDaoMapper");


		//数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setDbType(DbType.MYSQL);
		dsc.setUrl(url);
		dsc.setDriverName(driverClass);
		dsc.setUsername(userName);
		dsc.setPassword(passWord);
		mpg.setDataSource(dsc);


		//包配置
		PackageConfig pc = new PackageConfig();
		pc.setParent(setParent);
		pc.setMapper("mapper").setService("service").setController("controller").setEntity("entity");
		mpg.setPackageInfo(pc);


		//自定义配置
		if(StringUtils.isNotEmpty(mapperPath)) {
			InjectionConfig cfg = new InjectionConfig() {
				@Override
				public void initMap() {
					// to do nothing
				}
			};
			List<FileOutConfig> focList = new ArrayList<>();
			//mapper.xml
			focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
				@Override
				public String outputFile(TableInfo tableInfo) {
					// 自定义输入文件名称
					return projectPath + mapperPath + tableInfo.getMapperName()  + StringPool.DOT_XML;
				}
			});
			cfg.setFileOutConfigList(focList);
			mpg.setCfg(cfg);
		}


		//模板配置
		TemplateConfig tc = new TemplateConfig();
		tc.setController("/templates/controller.java").setEntity("/templates/entity.java")
				.setMapper("/templates/mapper.java").setService("/templates/service.java")
				.setServiceImpl("/templates/serviceImpl.java").setXml(null);
		mpg.setTemplate(tc);


		//策略配置
		StrategyConfig strategy = new StrategyConfig();
		//下划线到驼峰的命名方式
		strategy.setNaming(NamingStrategy.underline_to_camel);
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);
		//使用lombok
		strategy.setEntityLombokModel(true);
		strategy.setRestControllerStyle(true);
		strategy.setInclude(TABLES);
		strategy.setControllerMappingHyphenStyle(true);
		//继承自定义的controller，service，impl，dao
		strategy.setSuperControllerClass("com.zhx.real.mybatisplus.business.BaseController");
		strategy.setSuperEntityClass("com.zhx.real.mybatisplus.business.BaseEntity");
		mpg.setTemplateEngine(new FreemarkerTemplateEngine());
		mpg.setStrategy(strategy);
		mpg.execute();
	}
}
